require "Common/define"
require "Logic/Config"
require "Common/functions"

ShopItem = {};
local this = ShopItem;

function ShopItem.initData(obj, data,isHeroShop)

    local itemObj = obj:GetChild("Btn_ShopItemCopy");
    local name = itemObj:GetChild("name")
    --local icon = itemObj:GetChild("icon")
    --local quality = itemObj:GetChild("quality")
    local item = itemObj:GetChild("item")
    --local num = itemObj:GetChild("num")
    local gold = itemObj:GetChild("gold")
    local price = itemObj:GetChild("price")
    --local sellNull = itemObj:GetChild("mask")
    --库存
    --local repertory = itemObj:GetChild("repertory")
    local discount = itemObj:GetChild("discount")
    local discountCtrl = itemObj:GetController("discountCtrl");
    local ifShowShelves = obj:GetController("ifShowShelves");
    local lockCtrl = itemObj:GetController("lockCtrl");
    local heroName = itemObj:GetChild("hero"):GetChild("heroName");
    local typeCtrl=itemObj:GetController("type")
    local typeCtrl2=obj:GetController("type")
    local breakTxt=itemObj:GetChild("tipsBreak")
    breakTxt.alpha = 1;
    this.UIRedDots=itemObj:GetChild("UIRedDots")
    local breakTran = itemObj:GetTransition("Effect_BreakTips")
    lockCtrl.selectedPage = "unlock";
    discountCtrl.selectedPage = "notshow";
    ifShowShelves.selectedIndex = 0;
    itemObj.touchable = true;
    breakTran:Stop(true,false)
    if data ~= nil then
        name.text = data["name"];
        local itemCtrl = CtrlManager.GetCtrl(PanelNames.Item);
        if itemCtrl ~= nil then
            local dataInfo = {};
            dataInfo["itemModelId"] = data.itemModelId;
            dataInfo["num"] = data.num;
            dataInfo["icon"] = data.icon;
            dataInfo["isLock"] = not logicMgr.CostManager.itemIsOpen(data.itemModelId);
            itemCtrl.initData(item, dataInfo);
        end
        --icon.url =  EnumConst.AtlasPath.ItemAtlas..data["icon"];

        --if data["quality"] == 0 then
        --    data["quality"] = 1;
        --end
        --local iconQuality = Config.IconQuality[data["quality"]];
        --if iconQuality == nil then
        --    iconQuality = Config.IconQuality[#Config.IconQuality];
        --end
        --quality.url =  EnumConst.AtlasPath.ICONAtlas..iconQuality;

        --if data["num"] ~= nil then
        --    num.visible  = true;
        --    num.text = data["num"];
        --else
        --    num.visible  = false;
        --end
        if data["showCounter"] then
            ifShowShelves.selectedIndex = 1;
        end
        --金币类型
        if data["moneyIcon"] ~= nil then
            gold.visible = true;
            gold.url = EnumConst.AtlasPath.ItemAtlas .. data["moneyIcon"];
        else
            gold.visible = false;
        end
        -- 装备推荐
        this.UIRedDots.visible = false;
        if data.itemType == EnumConst.ItemTypeEnum.Equip then
            local heroList = logicMgr.EquipManager.GetNeedEquipHeroListByData(logicMgr.ShopManager.equipBaseInfo(data))
            if #heroList > 0 then
                this.UIRedDots.visible = true;
            end
        end
        -- 装备升阶石推荐
        if data.itemType == EnumConst.ItemTypeEnum.EquipStepstone then
            local heroList = logicMgr.ItemManager.GetNeedHeroListByData(data)
            if #heroList > 0 then
                this.UIRedDots.visible = true;
            end
        end
        --价格
        if data["price"] ~= nil then
            price.visible = true;
            local hadNum = logicMgr.ItemManager.getBagNumByTableID(tonumber(data["monyType"]));
            if hadNum ~= nil and tonumber(data["price"]) > tonumber(hadNum) then
                local costNum = data["price"]
                price.text = string.TextColor(getNumString(costNum), "#FF0000");
                this.UIRedDots.visible = false; -- 买不起不显示装备推荐红点
            else
                price.text = getNumString(data["price"]);
            end
        else
            price.visible = false;
        end

        ----库存
        --if data["remainCount"] ~= nil then
        --    if tonumber(data["remainCount"]) > 0 then
        --        if tonumber(data["remainCount"]) == 1 then
        --            repertory.visible  = false
        --        else
        --            repertory.visible  = true
        --            repertory.text = "库存："..data["remainCount"];
        --        end
        --        sellNull.visible  = false;
        --        itemObj.enabled  = true;
        --    else
        --        repertory.visible  = false
        --        sellNull.visible  = true;
        --        itemObj.enabled  = false;
        --    end
        --else
        --    repertory.visible  = false
        --end
        --折扣
        if data["discount"] ~= nil and tonumber(data["discount"]) ~= 10000 then
            discountCtrl.selectedPage = "show";
            local count = (10000 - tonumber(data["discount"])) / 100;
            discount.text = tostring(count);
        end
        itemObj.onClick:Clear();
        itemObj.onClick:Add(function()
            if logicMgr.CostManager.itemIsOpen(data.itemModelId, true) then
                if data.itemType == EnumConst.ItemTypeEnum.Equip then
                    local shopEquipAllInfo = logicMgr.ShopManager.equipBaseInfo(data);
                    if data["specialBuyType"] ~= nil then
                        shopEquipAllInfo["specialBuyType"] = data["specialBuyType"];
                    end
                    showUI(PanelNames.UIEquipInfo, nil, shopEquipAllInfo, 2);
                else
                    showUI(PanelNames.UIBagItemInfo, data, 2);
                end
            end
        end);
        if isHeroShop then
            typeCtrl.selectedIndex=1
            typeCtrl2.selectedIndex=1
        else
            typeCtrl.selectedIndex=0
            typeCtrl2.selectedIndex=0
        end
        if data["remainCount"] == 0 then
            lockCtrl.selectedIndex = 2;
            this.UIRedDots.visible = false; -- 卖完不显示装备推荐红点
            itemObj.onClick:Clear();
        elseif data["windowCondition"] ~= nil and data["isLock"] then
            itemObj.onClick:Clear();
            itemObj.touchable = false;
            lockCtrl.selectedPage = "lock";
            local heroData = G.dataTable["t_hero"][tostring(data["windowCondition"][2])];
            heroName.text = string.TextColor(getLanguage(heroData.f_HeroName), Config.ColorValue[logicMgr.HeroManager.starToQuality(heroData.f_HeroInitialStar)]);
        else
            lockCtrl.selectedPage = "unlock";
            --sellNull.visible  = false;
            itemObj.touchable = true;
            if logicMgr.ItemManager.getItemTabType(data.itemModelId)== EnumConst.ItemTableEnum.Hero then
                local haveNum=logicMgr.ItemManager.getItemCountbyModelId(data.itemModelId)

                local hero=logicMgr.HeroManager.getHeroDatabyModelId(tonumber(data.itemModelId)%600000, true)
                local curSerchID=hero["partnerModelId"]*1000+hero.star
                local config = G.dataTable["t_herorank"][tostring(curSerchID)]
                local quality=getLanguage("Sys_Quality"..tostring(Mathf.Floor( hero.star/10+1)) )
                quality=string.TextColor(quality,Config.ColorValue[logicMgr.HeroManager.starToQuality(hero.star+10)])
                --breakTxt.text=string.format(getLanguage("Sys_ShopBreakTips"),quality,tostring(haveNum),tostring(config.f_RankUpCost))
                local nextStar = G.dataTable["t_herorank"][tostring(config.f_HeroStarNext)].f_HeroStar
                if haveNum>=config.f_RankUpCost then
                    if nextStar > math.floor(nextStar / 10)*10 then
                        breakTxt.text = getLanguage("Sys_ShopBreakTips5")
                    else
                        breakTxt.text=string.format(getLanguage("Sys_ShopBreakTips1"),quality)
                    end
                    breakTran:Play(-1,0,0,-1,function()  end)
                else
                    local offset=config.f_RankUpCost-haveNum
                    if nextStar > math.floor(nextStar / 10)*10 then
                        breakTxt.text = string.format(getLanguage("Sys_ShopBreakTips4"),tostring(offset))
                    else
                        breakTxt.text=string.format(getLanguage("Sys_ShopBreakTips2"),tostring(offset),quality)
                    end
                end
            end
        end
    end
end

return ShopItem;